import javax.imageio.stream.ImageInputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:何人亮
 * Date:2024-12-07
 * 11:09
 */
  class ListNode {
   int val;
   ListNode next = null;
   public ListNode(int val) {
     this.val = val;
   }

//    public ListNode(BigInteger ret) {
//    }
}
public class Solution {

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param head1 ListNode类
     * @param head2 ListNode类
     * @return ListNode类
     */
    public static void main(String[] args) {


//        [9,3,7,3,0,8,4,4,4,4,4
        ListNode cur1=new ListNode(9);
        cur1.next=new ListNode(3);
        cur1.next.next=new ListNode(7);
        cur1.next.next.next=new ListNode(3);
        cur1.next.next.next.next=new ListNode(0);
        cur1.next.next.next.next.next=new ListNode(8);
        cur1.next.next.next.next.next.next=new ListNode(4);
        cur1.next.next.next.next.next.next.next=new ListNode(4);
        cur1.next.next.next.next.next.next.next.next=new ListNode(4);
        cur1.next.next.next.next.next.next.next.next.next=new ListNode(4);
        cur1.next.next.next.next.next.next.next.next.next.next=new ListNode(4);
        ListNode cur2=new ListNode(8);
        ListNode cur=addInList(cur1,cur2);
        System.out.println(cur.val);
    }
    public static ListNode  addInList (ListNode head1, ListNode head2) {

        // write code here
        Stack<BigInteger> stack=new Stack<>();
        BigInteger a1=BigInteger.ZERO;
        BigInteger a2=BigInteger.ZERO;
        int count1=0,count2=0;
        ListNode cur1=head1;
        ListNode cur2=head2;
        while(cur1!=null){
            count1++;
            cur1=cur1.next;
        }
        while(cur2!=null){
            count2++;
            cur2=cur2.next;
        }
        cur1=head1;
        cur2=head2;
        while(cur1!=null){
            int b=cur1.val;
            BigInteger b1=new BigInteger(String.valueOf(b));
            BigDecimal pow=new BigDecimal(Math.pow(10,count1-1));
            BigInteger b2=new BigInteger(String.valueOf(pow));
            a1=a1.add(b1.multiply(b2));
            //a1+=b*Math.pow(10,count1-1);

//            a1=a1.add(new BigInteger(String.valueOf(b)).multiply(new BigInteger(String.valueOf(Math.pow(10,count1-1)))));
            count1--;
            cur1=cur1.next;
        }
        while(cur2!=null){
            int b=cur2.val;
            //a1+=b*Math.pow(10,count2-1);
            BigInteger b1=new BigInteger(String.valueOf(b));
            BigDecimal pow=new BigDecimal(Math.pow(10,count2-1));
            BigInteger b2=new BigInteger(String.valueOf(pow));
            a2=a2.add(b1.multiply(b2));
            count2--;
            cur2=cur2.next;
        }
        BigInteger a3=a1.add(a2);
        while(a3!=BigInteger.ZERO){
            boolean add = stack.add(a3.remainder(new BigInteger(String.valueOf(10))));
//            a3/=10;
            a3=a3.divide(new BigInteger("10"));
        }
        int size=stack.size();
        ListNode head=new ListNode(-1);
        ListNode cur=head;
        for(int i=0;i<size;i++){
            BigInteger ret=stack.pop();

            head.next=new ListNode(Integer.parseInt(ret.toString()));
            head=head.next;
        }
        return cur.next;
    }

}
